package com.danielepelagatti.flickrmanager.commands
{
	import com.adobe.cairngorm.model.ModelLocator;
	import com.adobe.webapis.flickr.Photo;
	
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import com.theriabook.utils.Logger;
	/**
	 * 
	 * @author dpelagatti
	 * 
	 */	
	public class CommandHelper implements ModelLocator
	{
		/**
		* 
		*/		
		public static var pendingPhotoOperations:Dictionary = new Dictionary();
		/**
		* 
		*/		
		public static var donePhotoOperations:Dictionary = new Dictionary();
		/**
		 * 
		 * @param type
		 * @param photo
		 * 
		 */		
		public static function addPhotoOperation(type:String,photo:Photo):void
		{
			if( pendingPhotoOperations [ type ] == null)
				pendingPhotoOperations [ type ] = new Dictionary();
			
			if(pendingPhotoOperations [ type ] [ photo.id ] != null)
			{
				Logger.error("CommandHelper:: pending operation [ "+type+"] [ "+photo.id+" ] already exists");
				return;
			}
			
			pendingPhotoOperations [ type ] [ photo.id ] = photo;
			Logger.debug("CommandHelper:: Added pending operation [ "+type+" ] [ "+photo.id+" ]");
		}
		/**
		 * 
		 * @param type
		 * @param photo
		 * 
		 */		
		public static function removePhotoOperation(type:String,photo:Photo):void
		{
			if( pendingPhotoOperations [ type ] != null )
			{
				if( pendingPhotoOperations [ type ] [ photo.id ] != null )
				{
					delete pendingPhotoOperations [ type ] [ photo.id ];
					
					if(type.toLowerCase().indexOf("set") == -1)
					{
						if( donePhotoOperations [ type ] == null )
									donePhotoOperations [ type ] = new Dictionary();	
						donePhotoOperations [ type ] [ photo.id ] = photo;						
					}
	
					
					Logger.debug("CommandHelper:: Removed pending operation [ "+type+" ] [ "+photo.id+" ]");
				}
				else
					Logger.error("CommandHelper:: [ "+type+" ] [ "+photo.id+" ] does not exists");
			}
			else
				Logger.error("CommandHelper:: operation type [ "+type+" ] does not exists");
		}
		/**
		 * 
		 * @param type
		 * @param photo
		 * 
		 */		
		public static function getPhotoForOperation(type:String,photo_id:String):Photo
		{
			if( pendingPhotoOperations [ type ] != null )
			{
				if( pendingPhotoOperations [ type ] [ photo_id ] != null )
				{
					return Photo( pendingPhotoOperations [ type ] [ photo_id ] );
				}
				else
					Logger.error("CommandHelper:: [ "+type+" ] [ "+photo_id+" ] does not exists");
			}
			else
				Logger.error("CommandHelper:: operation type [ "+type+" ] does not exists");
			
			return null;		
		}
		/**
		 * 
		 * @return 
		 * 
		 */		
		public static function isOperationDone(type:String,photo_id:String):Boolean
		{
			if( donePhotoOperations [ type ] != null)
			{
				if( donePhotoOperations [ type ] [ photo_id ] )
					return true;
			}
			return false;
		}
	}
}